package com.sj.mapper;

import com.sj.entity.Comment;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author 小七
 * @since 2021-07-30
 */
@Repository
public interface CommentMapper extends BaseMapper<Comment> {

    /**
     * 查询parentId为null的评论
     *
     * @param blogId 博客id
     * @return parentId为null的评论列表
     */
    @Select("select * from comment where blog_id = #{blogId} and `status` = 1 and ISNULL(parent_comment_id) and `checked` = 1 order by create_time asc")
    @Results({
            @Result(column = "user_id", property = "user", one = @One(select = "com.sj.mapper.UserMapper.getUserById", fetchType = FetchType.EAGER)),
            @Result(column = "blog_id", property = "blog", one = @One(select = "com.sj.mapper.BlogMapper.selectById", fetchType = FetchType.EAGER))
    })
    List<Comment> listCommentWhereParentIdIsNull(@Param("blogId") Integer blogId);

    /**
     * 根据上一级评论id查找评论
     *
     * @param id 上一级评论id
     * @return 评论列表
     */
    @Select("select * from comment where parent_comment_id = #{id} and status = 1 and checked = 1")
    @Results({
            @Result(column = "parent_comment_id", property = "parentComment", one = @One(select = "com.sj.mapper.CommentMapper.selectById", fetchType = FetchType.EAGER)),
            @Result(column = "user_id", property = "userId"),
            @Result(column = "user_id", property = "user", one = @One(select = "com.sj.mapper.UserMapper.getUserById", fetchType = FetchType.EAGER))
    })
    List<Comment> getCommentByParentId(Integer id);
}
